package com.idp.ebay.test;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiCredential;
import com.ebay.sdk.ApiException;
import com.ebay.sdk.SdkException;
import com.ebay.sdk.call.GetOrdersCall;
import com.ebay.soap.eBLBaseComponents.DetailLevelCodeType;
import com.ebay.soap.eBLBaseComponents.OrderType;
import com.ebay.soap.eBLBaseComponents.SiteCodeType;

public class GetOrders {
	private ApiContext apiContext = new ApiContext();
	private ApiCredential cred = this.apiContext.getApiCredential();
	private int lastDay;
	private OrderType[] orders;
	private SiteCodeType SiteCode;
	private String Token;
	private String ApiServerUrl = "https://api.ebay.com/wsapi";
	
	public boolean setApiContext() {
		this.cred.seteBayToken(this.Token);
		this.apiContext.setApiServerUrl(this.ApiServerUrl);
		this.apiContext.getApiLogging().setLogSOAPMessages(true);
		this.apiContext.setSite(this.SiteCode);
		return true;
	}
	
	public boolean Get() throws ApiException, SdkException, Exception {
		
		/** +++++ ������ +++++ */
		DetailLevelCodeType[] detailLevels = new DetailLevelCodeType[] {
		          DetailLevelCodeType.RETURN_ALL
		      };
		GetOrdersCall Odsc = new GetOrdersCall(this.apiContext);
		Odsc.setDetailLevel(detailLevels);
		Calendar from = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
		Calendar to  = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
		from.add(Calendar.DAY_OF_MONTH, this.lastDay);
		Odsc.setCreateTimeFrom(from);
		Odsc.setCreateTimeTo(to);
		
		OrderType[] allOrders = Odsc.getOrders();
		if (allOrders != null) {
			this.orders = allOrders;
			return true;
		} else {
			return false;
		}
	}

	public void setLastDay(int lastDay) {
		this.lastDay = lastDay;
	}


	public void setSiteCode(SiteCodeType siteCode) {
		this.SiteCode = siteCode;
	}


	public void setToken(String token) {
		this.Token = token;
	}


	public OrderType[] getOrders() {
		return this.orders;
	}
}